package com.wang.lottery.infrastucture.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wang.lottery.common.Constants;
import com.wang.lottery.domain.rule.model.aggregates.TreeRuleRich;
import com.wang.lottery.domain.rule.model.po.RuleTree;
import com.wang.lottery.domain.rule.model.po.RuleTreeNode;
import com.wang.lottery.domain.rule.model.po.RuleTreeNodeLine;
import com.wang.lottery.domain.rule.model.vo.TreeNodeVO;
import com.wang.lottery.domain.rule.model.vo.TreeRootVO;
import com.wang.lottery.domain.rule.repository.IRuleRepository;
import com.wang.lottery.infrastucture.mapper.RuleTreeMapper;
import com.wang.lottery.infrastucture.mapper.RuleTreeNodeLineMapper;
import com.wang.lottery.infrastucture.mapper.RuleTreeNodeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wjh
 * @create 2023-11-11 15:59
 * 规则信息仓储服务
 */
@Repository
public class RuleRepository implements IRuleRepository {
    @Resource
    private RuleTreeMapper ruleTreeMapper;
    @Resource
    private RuleTreeNodeMapper ruleTreeNodeMapper;
    @Resource
    private RuleTreeNodeLineMapper ruleTreeNodeLineMapper;
    
    
    /**
     * 查询规则决策树配置（就是构造一颗决策树）
     *
     * @param treeId 决策树id
     * @return 决策树配置
     */
    @Override
    public TreeRuleRich queryTreeRuleRich(Long treeId) {
        //先查询决策树信息
        RuleTree ruleTree = ruleTreeMapper.selectById(treeId);
        //再根据决策树id查询该决策树下所有节点
        LambdaQueryWrapper<RuleTreeNode> queryWrapper = Wrappers.<RuleTreeNode>lambdaQuery().eq(RuleTreeNode::getTreeId, treeId);
        List<RuleTreeNode> ruleTreeNodeList = ruleTreeNodeMapper.selectList(queryWrapper);
        Map<Long, TreeNodeVO> treeNodeMap = new HashMap<>();
        //遍历每个节点
        for (RuleTreeNode ruleTreeNode : ruleTreeNodeList) {
            TreeNodeVO treeNodeVO = new TreeNodeVO();
            BeanUtils.copyProperties(ruleTreeNode,treeNodeVO);
            if (ruleTreeNode.getNodeType().equals(Constants.NodeType.STEM)) {
                //如果是非叶子节点，就说明它接下来又茎，根据这个节点的id和当前决策树id就可以查到当前节点的所有茎
                LambdaQueryWrapper<RuleTreeNodeLine> lineLambdaQueryWrapper = Wrappers.<RuleTreeNodeLine>lambdaQuery().eq(RuleTreeNodeLine::getNodeIdFrom, ruleTreeNode.getId())
                        .eq(RuleTreeNodeLine::getTreeId, treeId);
                //ruleTreeNodeLineList就是当前非叶子节点的所有茎的集合，把它放到当前节点VO中
                List<RuleTreeNodeLine> ruleTreeNodeLineList = ruleTreeNodeLineMapper.selectList(lineLambdaQueryWrapper);
                treeNodeVO.setRuleTreeNodeLineList(ruleTreeNodeLineList);
                treeNodeVO.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
            }
            //如果是叶子节点就直接put就行
            treeNodeMap.put(treeNodeVO.getId(),treeNodeVO);
        }
        //最后创建树根节点信息，封装数据返回
        TreeRootVO treeRootVO = new TreeRootVO();
        treeRootVO.setTreeId(treeId);
        treeRootVO.setTreeName(ruleTree.getTreeName());
        treeRootVO.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
        TreeRuleRich treeRuleRich = new TreeRuleRich();
        treeRuleRich.setTreeRoot(treeRootVO);
        
        treeRuleRich.setTreeNodeMap(treeNodeMap);
        return treeRuleRich;
    }
}
